﻿using Smobiler.Core;
using Smobiler.Core.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using DAL;
using FubiManger.UI;

namespace FubiManger.Pad
{
    ////ToolboxItem用于控制是否添加自定义控件到工具箱，true添加，false不添加
    //[System.ComponentModel.ToolboxItem(true)]
    partial class Pdiscountcontrol : Smobiler.Core.Controls.MobileUserControl
    {
        public Pdiscountcontrol() : base()
        {
            //This call is required by the SmobilerUserControl.
            InitializeComponent();
        }
        /// <summary>
        /// 商户ID
        /// </summary>
        public string merchantid { get; set; }
        public double discount_rate { get; set; }
        /// <summary>
        /// 用户手机
        /// </summary>
        public string usertel { get; set; }
        /// <summary>
        /// 订单号
        /// </summary>
        public string ordsn { get; set; }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnquery_Press(object sender, EventArgs e)
        {
            if (txtphone.Text.Length == 11)
            {
                usertel = txtphone.Text;//获取手机号
                ms_user user = userDal.Querybyphone(txtphone.Text);
                if (!string.IsNullOrEmpty(user.id))
                {
                    int money = orderDal.Querysummoneybyuid(user.id, merchantid);
                    ms_merchant_level level = merchant_levelDal.Querybymerchantiduid(merchantid, money.ToString());
                    if (level != null)
                    {
                        discount_rate = level.discount_rate * 0.01;//折扣
                        lbldiscount.Text = "折扣：" + discount_rate + "折";
                    }
                    else
                    {
                        lbldiscount.Text = "折扣：无折扣";
                        discount_rate = 1;
                    }
                }
                else
                {
                    string pwd = AesUtil.EncryptString("123456");
                    if (userDal.Reguser(txtphone.Text, pwd) > 0)
                    {
                        user = userDal.Querybyphone(txtphone.Text);
                        orderDal.Updateusertel(ordsn, user.username, user.id);//修改订单用户信息
                        int money = orderDal.Querysummoneybyuid(user.id, merchantid);//获取金额
                        ms_merchant_level level = merchant_levelDal.Querybymerchantiduid(merchantid, money.ToString());
                        if (level != null)
                        {
                            discount_rate = level.discount_rate * 0.01;//折扣
                            lbldiscount.Text = "折扣：" + discount_rate + "折";
                        }
                        else
                        {
                            lbldiscount.Text = "折扣：无折扣";
                            discount_rate = 1;
                        }                           
                    }
                }
            }
        }
        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnsub_Press(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnclose_Press(object sender, EventArgs e)
        {
            discount_rate = 0;
            this.Close();
        }
    }
}